clear

u ${bhps}\clifejob
recode cljbgm -1=7 13=1 14=4 15=7 16=10
recode cljlftm -1=7 13=1 14=4 15=7 16=10
mvdecode cljlft* cljbg*, mv(-9)
mvdecode cljlft* cljbg*, mv(-2)
mvdecode cljlft* cljbg*, mv(-1)


/** beginning date ***/
g bg=mdy(cljbgm,1,cljbgy+1900)/365

/** end date ***/
g lft=mdy(cljlftm,1,cljlfty+1900)/365 if cljlfty~=-8

g sept190=mdy(9,1,1990)/365
g sept191=mdy(9,1,1991)/365
g sept192=mdy(9,1,1992)/365
g sept193=mdy(9,1,1993)/365

sort chid cpno cljseq
quie by chid cpno: g diff=lft-bg[_n+1]

/* works out beginning date of job at 1.9.90 */
g bgsept=bg-sept190
egen jmax=max(cljseq) if bg~=., by(chid cpno)
sort chid cpno cljseq
quie by chid cpno: g z90=(bg<sept190)&(bg[_n+1]>=sept190)&bg[_n+1]~=.
g z190=(bg<sept190)&(cljseq==jmax)&((lft>sept190)|cljlfty==-8)
g z290=z90+z190
g bgz90=bg if z290==1

/* works out beginning date of job at 1.9.91 */
sort chid cpno cljseq
quie by chid cpno: g z91=(bg<sept191)&(bg[_n+1]>=sept191)&bg[_n+1]~=.
g z191=(bg<sept191)&(cljseq==jmax)&((lft>sept191)|cljlfty==-8)
g z291=z91+z191
g bgz91=bg if z291==1

/* works out beginning date of job at 1.9.92 */
sort chid cpno cljseq
quie by chid cpno: g z92=(bg<sept192)&(bg[_n+1]>=sept192)&bg[_n+1]~=.
g z192=(bg<sept192)&(cljseq==jmax)&((lft>sept192)|cljlfty==-8)
g z292=z92+z192
g bgz92=bg if z292==1

/* works out beginning date of job at 1.9.93 */
sort chid cpno cljseq
quie by chid cpno: g z93=(bg<sept193)&(bg[_n+1]>=sept193)&bg[_n+1]~=.
g z193=(bg<sept193)&(cljseq==jmax)&((lft>sept193)|cljlfty==-8)
g z293=z93+z193
g bgz93=bg if z293==1

egen bgl90=max(bgz90), by(chid cpno)
egen bgl91=max(bgz91), by(chid cpno)
egen bgl92=max(bgz92), by(chid cpno)
egen bgl93=max(bgz93), by(chid cpno)
g liferec=1
collapse bgl90 bgl91 bgl92 bgl93 sept190 liferec , by(chid cpno)
rename chid hid
rename cpno pno
sort hid pno
save ${data}\life, replace






